using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class InstantiateSegment : MonoBehaviour {
	public Texture2D test;
	public GameObject wallPrefab, floorPrefab;
	public GUITexture guitext;
	
	private const string whiteString = "RGBA(1.000, 1.000, 1.000, 1.000)";
	private const string redString = "RGBA(1.000, 0.000, 0.000, 1.000)";
	private const string blueString = "RGBA(0.000, 0.000, 1.000, 1.000)";
	private const string greenString = "RGBA(0.000, 1.000, 0.000, 1.000)";
	
	/* White = blank or wall
	 * Red = transition
	 * Blue = floor
	 * Green = inside transition
	 **/

	void Start () {
		//for (int i = 0; i < test.width; i++) {
		//	for (int d = 0; d < test.height; d++) {
		//		print(i + "," + d + " = " + test.GetPixel(i,d));
		//	}
		//}
		//test = lib.rotateTexture(test, 270);
		//guitext.texture = test;
		testInstantiation();
		//test.
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void testInstantiation(){
		
		for (int i = 0; i < test.width; i++) {
			for (int d = 0; d < test.height; d++) {
				switch (test.GetPixel(i, d).ToString()) {
				case whiteString:
					break;
				case redString:
					print("red");
					Instantiate(floorPrefab, new Vector3(i, 0, d), Quaternion.identity);
					if((i == 0 || test.GetPixel(i-1, d) == Color.white) && test.GetPixel(i+1, d) == Color.red){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(180, Vector3.up));
					}
					if((i == test.width-1 || test.GetPixel(i+1, d) == Color.white) && test.GetPixel(i-1, d) == Color.red){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(0, Vector3.up));
					}
					if((d == 0 || test.GetPixel(i, d-1) == Color.white) && test.GetPixel(i, d+1) == Color.red){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(90, Vector3.up));
					}
					if((d == test.height-1 || test.GetPixel(i, d+1) == Color.white) && test.GetPixel(i, d-1) == Color.red){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(270, Vector3.up));
					}
					break;
				case blueString:
				case greenString:
					Instantiate(floorPrefab, new Vector3(i, 0, d), Quaternion.identity);
					if(i == 0 || test.GetPixel(i-1, d) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(180, Vector3.up));
					}
					if(i == test.width-1 || test.GetPixel(i+1, d) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(0, Vector3.up));
					}
					if(d == 0 || test.GetPixel(i, d-1) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(90, Vector3.up));
					}
					if(d == test.height-1 || test.GetPixel(i, d+1) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(270, Vector3.up));
					}
					break;
				default:
					Debug.LogWarning("Coordinate " + i + "," + d + " had unexpected color " + test.GetPixel(i, d));
				break;
				}
				
				/*
				if(test.GetPixel(i, d) != Color.white){
					if(i == 0 || test.GetPixel(i-1, d) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(180, Vector3.up));
					}
					if(i == test.width-1 || test.GetPixel(i+1, d) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(0, Vector3.up));
					}
					if(d == 0 || test.GetPixel(i, d-1) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(90, Vector3.up));
					}
					if(d == test.width-1 || test.GetPixel(i, d+1) == Color.white){
						Instantiate(wallPrefab, new Vector3(i, 0, d), Quaternion.AngleAxis(270, Vector3.up));
					}
				}
				*/
			}
		}
	}
}
